package com.j180.dao.user;

import com.j180.bean.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IUserDao {
    /**
     * 用户登录
     * @param username 用户
     * @param pwd 密码（用md5加密）
     * @return null 表示失败，不是null表示
     */
    @Select("SELECT " +
            " u_id AS id, " +
            " u_loginName AS loginName, " +
            " u_password AS password, " +
            " u_telNum AS telNum, " +
            " u_email AS email, " +
            " u_name AS name, " +
            " u_identity AS identity, " +
            " u_state AS state " +
            " FROM " +
            " t_user " +
            " WHERE " +
            " u_loginName = #{username} " +
            " AND u_password = #{pwd}")
    User userLogin(@Param("username") String username, @Param("pwd") String pwd);

    /**
     * 用户注册 注册后状态为待审核
     * @param user 注册的用户信息
     * @return 0 失败，  1 成功
     */
    @Insert("INSERT INTO t_user " +
            "VALUES " +
            " ( " +
            " NULL, " +
            " #{loginName}," +
            " #{password}, " +
            " #{telNum}, " +
            " #{email}, " +
            " #{name}, " +
            " #{identity}, " +
            " '待审核' " +
            " ); ")
    int userRegister(User user);

    /**
     * 设置用户状态
     *  1. 平台管理员在审核后修改用户的状态（通过与不通过）
     *  2. 当平台管理员对某用户进行禁用是将该用户的状态改为不可用
     * @param state 用户状态
     * @param id 用户id
     * @return 1 成功 ， 0 失败
     */
    @Update("update t_user set " +
            " u_state = #{state} " +
            "where u_id = #{id}")
    int setUserState(@Param("state") String state, @Param("id") int id);

    /**
     * 查询所有用户信息
     * @return 用户集合
     */
    @Select("SELECT " +
            "u_id AS id, " +
            "u_loginName AS loginName, " +
            "u_password AS PASSWORD, " +
            "u_telNum AS telNum, " +
            "u_email AS email, " +
            "u_name AS NAME, " +
            "u_identity AS identity, " +
            "u_state AS state " +
            "FROM " +
            " t_user ")
    List<User> getAllUsers();

    /**
     * 根据id查询用户信息
     * @param id 用户id
     * @return 用户类
     */
    @Select("SELECT " +
            "u_id AS id, " +
            "u_loginName AS loginName, " +
            "u_password AS PASSWORD, " +
            "u_telNum AS telNum, " +
            "u_email AS email, " +
            "u_name AS NAME, " +
            "u_identity AS identity, " +
            "u_state AS state " +
            "FROM " +
            " t_user where u_id = #{id}")
    User getUserById(@Param("id") int id);

    /**
     *  修改用户信息
     * @param user 修改后的用户的所有信息
     * @return 1 成功 ， 0 失败
     */
    @Update("update t_user set " +
            "u_loginName = #{loginName}," +
            " u_password = #{password}," +
            " u_telNum = #{telNum}," +
            " u_email = #{email}," +
            " u_name = #{name}," +
            " u_identity = #{identity}," +
            " u_state = #{state} " +
            "where u_id = #{id}")
    int updateUser(User user);

    /**
     * 删除id所对应的用户信息
     * @param id
     * @return
     */
    @Delete("delete from t_user where u_id = #{id}")
    int deleteById(@Param("id") int id);

    /**
     * 查询注册未审核的用户
     * @param state
     * @return
     */
    @Select("SELECT " +
            "u_id AS id, " +
            "u_loginName AS loginName, " +
            "u_password AS PASSWORD, " +
            "u_telNum AS telNum, " +
            "u_email AS email, " +
            "u_name AS NAME, " +
            "u_identity AS identity, " +
            "u_state AS state " +
            "FROM " +
            " t_user where u_state = #{state}")
    List<User> getRegisterUser(@Param("state") String state);

}